$(function(){
    "use strict";
    /**
     *全选的功能
     */
    $('#checkAll').on('click',function(){
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for(var i=0;i<checkboxs.length;i++){
            var checkbox=checkboxs[i];
            checkbox.checked=this.checked;
        }
    })
    $('#table_table').on('click','input[type=checkbox]:not("#checkAll")',function(){
        "use strict";
        var isCheckedAll = true;
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                isCheckedAll = false;
                break;
            }
        }
        $('#checkAll')[0].checked = isCheckedAll;
    })

    /* 当点击添加按钮时 */
    $('.operation_btn').on('click','a',function(e){
        e.preventDefault();
        e.stopPropagation();
        if( $(this).html() == '添加' ){
            let add_people = $(this).attr('href');
            $(`[lll=${add_people}]`).addClass('active')
        }
        if( $(this).html() == '批量删除' ){
            var checkboxs =$('input[type=checkbox]:not("#checkAll")');
            var len = 0;
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    continue
                }else{
                    len++
                }
            }
            if(len == checkboxs.length){
                $('#yz_modal').html('您还没有选择人员！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
            let _delete = $(this).attr('href');
            $(_delete).addClass('active')
        }
        if( $(this).html() == '批量导入' ){
            let showbox_id = $(this).attr('href');
            $(showbox_id).addClass('active')
        }
    })

    // 批量删除
    $('.delete_tip_btn_box').on('click','button',function(){
        "use strict";
        let delete_arr = [];
        if($(this).html() == '确认'){
            let checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            for (let i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    delete_arr.push( $(checkboxs[i]).closest('tr[data-index]').attr('data-index') )
                }else{
                    continue;
                }
            }
            $.ajax({
                url:'/Admin/Account/multiDelAccount',
                type:'post',
                data:{
                    acc_id: delete_arr
                },
                success:function(data){
                    if(data.code==1){
                        for(let r = 0; r < delete_arr.length; r++){
                            $('#table_table').children('tbody').children(`[data-index=${delete_arr[r]}]`).remove();
                        }
                        $('#delete').removeClass('active')
                        window.location.href = `/Admin/Account/index`//强制刷新
                    }
                }
            })
        }else{
            $('#delete').removeClass('active')
        }
    })

    // 当点击编辑按钮时
    $('#table_table').on('click','.edit_table',function(e){
        e.preventDefault();
        let operation = $(this).attr('href');
        let operation_id = $(this).parent().parent().attr('data-index');
        if( operation == 'edit_people'){
            $.ajax({
                url:'/Admin/Account/editAccount',
                type:'get',
                data:{
                    id: operation_id
                },
                success:function(data){
                    console.log(data)
                    if(data.code==1){
                        let datas = data.data;
                        $(`[lll=${operation}]`).find(`[add_name='edit_year']`).val(datas.year)
                        $(`[lll=${operation}]`).find(`[add_name='edit_Totalproperty']`).val(datas.total_price)
                        $(`[lll=${operation}]`).find(`[add_name='edit_registerCapital']`).val(datas.reg_price)
                        $(`[lll=${operation}]`).find(`[add_name='edit_RD_investment']`).val(datas.explore_price)
                        $(`[lll=${operation}]`).find(`[add_name='edit_sales_income']`).val(datas.shop_price)
                        $(`[lll=${operation}]`).find(`[add_name='edit_RD_Tosales_Proportion']`).val(((Number(datas.explore_price)/Number(datas.shop_price))*100).toFixed(2))
                        $(`[lll=${operation}]`).find(`[add_name='edit_cost']`).val(datas.cost_price)
                        $(`[lll=${operation}]`).find(`[add_name='edit_RD_Tocost_Proportion']`).val(((Number(datas.explore_price)/Number(datas.cost_price))*100).toFixed(2))
                    }
                }
            })

            window.sessionStorage['operation_id']= operation_id
            $(`[lll=${operation}]`).addClass('active');
        }
    })

    /**
     * 确认按钮点击时
     */
    // var tbodyLen = $('#table_table').children('tbody').length;
    $('#add_btn').click(function(){

        let add_year = $(this).closest('[lll]').find('[add_name=add_year]').val();
        let add_Totalproperty = $(this).closest('[lll]').find('[add_name=add_Totalproperty]').val();
        let add_registerCapital = $(this).closest('[lll]').find('[add_name=add_registerCapital]').val();
        let add_sales_income = $(this).closest('[lll]').find('[add_name=add_sales_income]').val();
        let add_RD_investment = Number($(this).closest('[lll]').find('[add_name=add_RD_investment]').val()).toFixed(2);
        let add_RD_Tosales_Proportion = $(this).closest('[lll]').find('[add_name=add_RD_Tosales_Proportion]').val();/**/
        let add_cost = $(this).closest('[lll]').find('[add_name=add_cost]').val();
        let add_RD_Tocost_Proportion = Number($(this).closest('[lll]').find('[add_name=add_RD_Tocost_Proportion]').val()).toFixed(2);/**/

        if(add_year == '' || add_Totalproperty == '' || add_registerCapital == '' || add_sales_income == '' || add_RD_investment == '' || add_cost == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        //防止重复点击按钮
            if( $(this).attr('lock') ) return
            $(this).attr('lock','lock')

        let that = this
        $.ajax({
            url:'/Admin/Account/addAccount',
            type:'post',
            data:{
                year: add_year,
                total_price: add_Totalproperty,
                reg_price: add_registerCapital,
                shop_price: add_sales_income,
                explore_price: add_RD_investment,
                cost_price: add_cost
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    // var peopleTableHTML = $('#table_table').children('tbody').html()
                    let peopleTableHTML = `
                                <tr data-index="${data.id}">
                                        <td><input type="checkbox" class="checkboxStyle"></td>
                                        <td data-name="edit_year">${add_year}</td>
                                        <td data-name="edit_Totalproperty">${add_Totalproperty}</td>
                                        <td data-name="edit_registerCapital">${add_registerCapital}</td>
                                        <td data-name="edit_sales_income">${add_sales_income}</td>
                                        <td data-name="edit_RD_investment">${add_RD_investment}</td>
                                        <td data-name="edit_RD_Tosales_Proportion">${add_RD_Tosales_Proportion}%</td>
                                        <td data-name="edit_cost">${add_cost}</td>
                                        <td data-name="edit_RD_Tocost_Proportion">${add_RD_Tocost_Proportion}%</td>
                                        <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                    </tr>
                        `
                    $('#table_table').children('tbody').prepend(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                    $(that).parent().parent().removeClass('active')
                    $(that).closest('[lll]').removeClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }

                $(that).removeAttr('lock')
            }
        })

    })
    /*研发占比销售 自动计算*/
    $('[add_name=add_sales_income]').blur(function(){
        let add_sales_income = $(this).val();
        let add_RD_investment = $(this).closest('[lll]').find('[add_name=add_RD_investment]').val();
        if(add_sales_income&&add_RD_investment){
            let add_RD_Tosales_Proportion = ((Number(add_RD_investment)/Number(add_sales_income))*100).toFixed(2)
            $(this).closest('[lll]').find('[add_name=add_RD_Tosales_Proportion]').val(add_RD_Tosales_Proportion)
        }else{
            $(this).closest('[lll]').find('[add_name=add_RD_Tosales_Proportion]').val('')
            return
        }
    })
    $('[add_name=add_RD_investment]').blur(function(){
        let add_RD_investment = $(this).val();
        let add_sales_income = $(this).closest('[lll]').find('[add_name=add_sales_income]').val();

        let add_cost = $(this).closest('[lll]').find('[add_name=add_cost]').val();
        if(add_sales_income&&add_RD_investment){
            let add_RD_Tosales_Proportion = ((Number(add_RD_investment)/Number(add_sales_income))*100).toFixed(2)
            $(this).closest('[lll]').find('[add_name=add_RD_Tosales_Proportion]').val(add_RD_Tosales_Proportion)
        }else{
            $(this).closest('[lll]').find('[add_name=add_RD_Tosales_Proportion]').val('')
            return
        }
        if(add_cost&&add_RD_investment){
            let add_RD_Tocost_Proportion = ((Number(add_RD_investment)/Number(add_cost))*100).toFixed(2)
            $(this).closest('[lll]').find('[add_name=add_RD_Tocost_Proportion]').val(add_RD_Tocost_Proportion)
        }else{
            $(this).closest('[lll]').find('[add_name=add_RD_Tocost_Proportion]').val('')
            return
        }
    })
    /*研发占比成本 自动计算*/
    $('[add_name=add_cost]').blur(function(){
        let add_cost = $(this).val();
        let add_RD_investment = $(this).closest('[lll]').find('[add_name=add_RD_investment]').val();
        if(add_cost&&add_RD_investment){
            let add_RD_Tocost_Proportion = ((Number(add_RD_investment)/Number(add_cost))*100).toFixed(2)
            $(this).closest('[lll]').find('[add_name=add_RD_Tocost_Proportion]').val(add_RD_Tocost_Proportion)
        }else{
            $(this).closest('[lll]').find('[add_name=add_RD_Tocost_Proportion]').val('')
            return
        }
    })

    /**
     * 确认并新增按钮点击时
     */
    $('#add_AddNew_btn').click(function(){

        let add_year = $(this).closest('[lll]').find('[add_name=add_year]').val();
        let add_Totalproperty = $(this).closest('[lll]').find('[add_name=add_Totalproperty]').val();
        let add_registerCapital = $(this).closest('[lll]').find('[add_name=add_registerCapital]').val();
        let add_sales_income = $(this).closest('[lll]').find('[add_name=add_sales_income]').val();
        let add_RD_investment = Number($(this).closest('[lll]').find('[add_name=add_RD_investment]').val()).toFixed(2);
        let add_RD_Tosales_Proportion = $(this).closest('[lll]').find('[add_name=add_RD_Tosales_Proportion]').val();/**/
        let add_cost = $(this).closest('[lll]').find('[add_name=add_cost]').val();
        let add_RD_Tocost_Proportion = Number($(this).closest('[lll]').find('[add_name=add_RD_Tocost_Proportion]').val()).toFixed(2);/**/

        if(add_year == '' || add_Totalproperty == '' || add_registerCapital == '' || add_sales_income == '' || add_RD_investment == '' || add_cost == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        //防止重复点击按钮
            if( $(this).attr('lock') ) return
            $(this).attr('lock','lock')

        let that = this
        $.ajax({
            url:'/Admin/Account/addAccount',
            type:'post',
            data:{
                year: add_year,
                total_price: add_Totalproperty,
                reg_price: add_registerCapital,
                shop_price: add_sales_income,
                explore_price: add_RD_investment,
                cost_price: add_cost
            },
            success:function(data){
                // console.log(data)
                if(data.code==1){
                    // var peopleTableHTML = $('#table_table').children('tbody').html()
                    let peopleTableHTML = `
                                <tr data-index="${data.id}">
                                        <td><input type="checkbox" class="checkboxStyle"></td>
                                        <td data-name="edit_year">${add_year}</td>
                                        <td data-name="edit_Totalproperty">${add_Totalproperty}</td>
                                        <td data-name="edit_registerCapital">${add_registerCapital}</td>
                                        <td data-name="edit_sales_income">${add_sales_income}</td>
                                        <td data-name="edit_RD_investment">${add_RD_investment}</td>
                                        <td data-name="edit_RD_Tosales_Proportion">${add_RD_Tosales_Proportion}%</td>
                                        <td data-name="edit_cost">${add_cost}</td>
                                        <td data-name="edit_RD_Tocost_Proportion">${add_RD_Tocost_Proportion}%</td>
                                        <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                    </tr>
                        `
                    $('#table_table').children('tbody').prepend(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
                $(that).removeAttr('lock')

            }
        })
    })

    /**
     * 保存按钮点击时
     */
    $('#save_btn').click(function(){
        if( $(this).html() == '保存' ){

            //防止重复点击按钮
            if( $(this).attr('lock') ) return
            $(this).attr('lock','lock')

            let edit_year = $(this).closest('[lll]').find('[add_name=edit_year]').val();
            let edit_Totalproperty = $(this).closest('[lll]').find('[add_name=edit_Totalproperty]').val();
            let edit_registerCapital = $(this).closest('[lll]').find('[add_name=edit_registerCapital]').val();
            let edit_sales_income = $(this).closest('[lll]').find('[add_name=edit_sales_income]').val();
            let edit_RD_investment = Number($(this).closest('[lll]').find('[add_name=edit_RD_investment]').val()).toFixed(2);
            let edit_RD_Tosales_Proportion = $(this).closest('[lll]').find('[add_name=edit_RD_Tosales_Proportion]').val();/**/
            let edit_cost = $(this).closest('[lll]').find('[add_name=edit_cost]').val();
            let edit_RD_Tocost_Proportion = Number($(this).closest('[lll]').find('[add_name=edit_RD_Tocost_Proportion]').val()).toFixed(2);/**/

            if(edit_year == '' || edit_Totalproperty == '' || edit_registerCapital == '' || edit_sales_income == '' || edit_RD_investment == '' || edit_cost == ''){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
            let operation_id = window.sessionStorage['operation_id']
            let that = this
            $.ajax({
                url:'/Admin/Account/editAccount',
                type:'post',
                data:{
                    id:operation_id,
                    year: edit_year,
                    total_price: edit_Totalproperty,
                    reg_price: edit_registerCapital,
                    shop_price: edit_sales_income,
                    explore_price: edit_RD_investment,
                    cost_price: edit_cost
                },
                success:function(data){
                    if(data.code==1){
                        var tableHTML = $('#table_table').children('tbody').find(`[data-index=${operation_id}]`)
                        tableHTML.children('[data-name=edit_year]').html(edit_year)
                        tableHTML.children('[data-name=edit_Totalproperty]').html(edit_Totalproperty)
                        tableHTML.children('[data-name=edit_registerCapital]').html(edit_registerCapital)
                        tableHTML.children('[data-name=edit_sales_income]').html(edit_sales_income)
                        tableHTML.children('[data-name=edit_RD_investment]').html(edit_RD_investment)
                        tableHTML.children('[data-name=edit_RD_Tosales_Proportion]').html(edit_RD_Tosales_Proportion+'%')
                        tableHTML.children('[data-name=edit_cost]').html(edit_cost)
                        tableHTML.children('[data-name=edit_RD_Tocost_Proportion]').html(edit_RD_Tocost_Proportion+'%')

                        $(that).parent().parent().removeClass('active')
                    }
                    $(that).removeAttr('lock')
                }
            })

        }

    })
    /*研发占比销售 自动计算*/
    $('[add_name=edit_sales_income]').blur(function(){
        let edit_sales_income = $(this).val();
        let edit_RD_investment = $(this).closest('[lll]').find('[add_name=edit_RD_investment]').val();
        if(edit_sales_income&&edit_RD_investment){
            let edit_RD_Tosales_Proportion = ((Number(edit_RD_investment)/Number(edit_sales_income))*100).toFixed(2)
            $(this).closest('[lll]').find('[add_name=edit_RD_Tosales_Proportion]').val(edit_RD_Tosales_Proportion)
        }else{
            $(this).closest('[lll]').find('[add_name=edit_RD_Tosales_Proportion]').val('')
            return
        }
    })
    $('[add_name=edit_RD_investment]').blur(function(){
        let edit_RD_investment = $(this).val();
        let edit_sales_income = $(this).closest('[lll]').find('[add_name=edit_sales_income]').val();

        let edit_cost = $(this).closest('[lll]').find('[add_name=edit_cost]').val();
        if(edit_sales_income&&edit_RD_investment){
            let edit_RD_Tosales_Proportion = ((Number(edit_RD_investment)/Number(edit_sales_income))*100).toFixed(2)
            $(this).closest('[lll]').find('[add_name=edit_RD_Tosales_Proportion]').val(edit_RD_Tosales_Proportion)
        }else{
            $(this).closest('[lll]').find('[add_name=edit_RD_Tosales_Proportion]').val('')
            return
        }
        if(edit_cost&&edit_RD_investment){
            let edit_RD_Tocost_Proportion = ((Number(edit_RD_investment)/Number(edit_cost))*100).toFixed(2)
            $(this).closest('[lll]').find('[add_name=edit_RD_Tocost_Proportion]').val(edit_RD_Tocost_Proportion)
        }else{
            $(this).closest('[lll]').find('[add_name=edit_RD_Tocost_Proportion]').val('')
            return
        }
    })
    /*研发占比成本 自动计算*/
    $('[add_name=edit_cost]').blur(function(){
        let edit_cost = $(this).val();
        let edit_RD_investment = $(this).closest('[lll]').find('[add_name=edit_RD_investment]').val();
        if(edit_cost&&edit_RD_investment){
            let edit_RD_Tocost_Proportion = ((Number(edit_RD_investment)/Number(edit_cost))*100).toFixed(2)
            $(this).closest('[lll]').find('[add_name=edit_RD_Tocost_Proportion]').val(edit_RD_Tocost_Proportion)
        }else{
            $(this).closest('[lll]').find('[add_name=edit_RD_Tocost_Proportion]').val('')
            return
        }
    })

    /**
     * 取消按钮点击时
     */
    $('.button_list').on('click','button',function(){
        if($(this).hasClass('cancel_btn')){
            $(this).closest('[lll]').removeClass('active')
        }
    })

    let msg = '文件正在上传，请勿重复点击！'

    // 上传文件
    $('#file_upload').change(function (e) {
        $(this).siblings('.import_tip_btn_box').find('button').removeClass('btn-grey').addClass('btn-success')
        msg = '文件正在上传，请勿重复点击！'
        $(this).siblings('.import_tip_btn_box').find('button').removeAttr('lock')
    })
    $('#import').find('button').click(function(e){
        "use strict";
        e.preventDefault()
        if($(this).hasClass('btn-success')){
            if($(this).html()=='确定导入'){
                //防止重复点击按钮
                if( $(this).attr('lock') ){
                    $('#yz_modal').html(msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                    return
                }
                $(this).attr('lock','lock')
                let that = this

                var formData = new FormData($("#file_form")[0]);
                $.ajax({
                    async : false,
                    cache : false,
                    type : "post",
                    data : formData,
                    url : '/Admin/Account/importAccount',
                    dataType : 'json',
                    contentType: false, //必须
                    processData: false, //必须
                    success : function(data) {
                        console.log(data);
                        if(data.code==1){
                            $('#import').removeClass('active')
                            window.location.href = `/Admin/Account/index`//强制刷新
                            $(that).removeAttr('lock')
                        }else{
                            // console.log(222)
                            // console.log($('#yz_modal').html())
                            msg = data.msg
                            $('#yz_modal').html(msg)
                            $('#yz_modal').addClass('active')
                            tipShow()
                        }
                    }
                })
            }
        }

    })


    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }

    //当点击区域不在导入弹框中时则关闭弹框
    $(document).click(function(e){
        let $this = $(e.target);
        if($this.closest('#import').length==0){
            if($this.attr('href') != '#import' && $this.children().attr('href') != '#import'){
                $('#import').removeClass('active');
            }
        }
    })
})